package py.com.fms.gnd.session.maintenances;

//External classes imports 
//Java Classes
import java.util.*;
import java.awt.Checkbox;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.faces.event.ActionEvent;

//Seam Classes
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage.Severity;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

//project classes imports

import py.com.fms.fms.bean.RefDetBean;
import py.com.fms.fms.bean.RefHeadBean;
import py.com.fms.fms.bean.UsersBean;
import py.com.fms.fms.persistence.services.RefServices;
import py.com.fms.fms.persistence.services.UsersServices;
import py.com.fms.gnd.bean.AfflicSympBean;
import py.com.fms.gnd.bean.AfflictionBean;
import py.com.fms.gnd.bean.RacesBean;
import py.com.fms.gnd.bean.SymptomBean;
import py.com.fms.gnd.persistence.services.AfflicSympServices;
import py.com.fms.gnd.persistence.services.AfflictionServices;
import py.com.fms.gnd.persistence.services.SymptomServices;
import py.com.fms.exception.CommonException;
import py.com.fms.session.ServiceSqlMap;
import py.com.fms.session.WithinHolder;
import py.com.fms.utilities.*;

@SuppressWarnings("unused")
@Name("editEnfermSintoma")
@Scope(ScopeType.SESSION)
public class EditAfflicSymp {
	
	private FacesMessages facesMessages;
	@Logger
	private Log log;
	@In("#{serviceSqlMap}")
	private ServiceSqlMap sqlMap;
	private String initVar;
	private Date today = null;
	
	// Task Attendees Attributes 
	@In("#{withinHolder}")
	private WithinHolder aid;
	@In("#{convertAssist}")
	private ConvertAssistant convert;
	private DateAssistant dateAssist;
	@In Credentials credentials;
	@In Identity identity;
	

	// Collection of Bean
	private Collection<AfflicSympBean> allAfflicSymp;

	// Attribute used for editing or updating
	
	
	// Beans
	private UsersBean user = null; // For User Data 
	private AfflicSympBean afflicSymp = null; // Used in main forms
	private AfflicSympBean editAfflicSymp = null; // Used for update operations
	
	// Services
	private AfflicSympServices affSympSrv;
	private AfflictionServices afflicSrv;
	private SymptomServices sympSrv;
	private UsersServices usSrv;
	
	
	//	Necessary Attributes;
	private AfflictionBean afflictionDesc;
	private String afflictionCod;
	private Long afflictionId;
	
	private SymptomBean symptomDesc;
	private String symptomCod;
	private Long symptomId;
	
	private List<SymptomBean> symptomList = new ArrayList<SymptomBean>();
	private List<Object> deleteList = new ArrayList<Object>();
	private boolean check;
	private Object remove;


	/**
	 * [[[[[[[[[[[[ Methods ]]]]]]]]]]]] 
	 * [[[[[[[[[[-------------]]]]]]]]]]this.initCollections();
			
			this.clearList();
	 */

	// Used to initialize information from a variable
	
	public void initData() throws CommonException {

		System.out.println("initVar = " + this.getInitVar());

		if ("S".equals(this.getInitVar())) {
			
			this.initBeans();
			this.initServices();
			this.initAssistants();
			this.initCollections();
			
			this.clearList();
			deleteList.clear();
			
			if(credentials != null){
				System.out.println("Credenciales Encontradas");
				this.getUser().setUserName(credentials.getUsername());
				System.out.println("User Name: " + this.getUser().getUserName());
				user = usSrv.getUserDataByName(this.getUser().getUserName());
				this.setUser(user);
				System.out.println("User: " + this.getUser());
			}else{	
				System.out.println("Credenciales Nulas");
			}
		}
		
	}
	
		
		// INITIALIZING --------------------]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
		
		public void initBeans(){
		
			if(afflicSymp == null){
				afflicSymp = new AfflicSympBean();
			}
			
			if(editAfflicSymp == null){
				editAfflicSymp = new AfflicSympBean();
			}
			
			if(afflictionDesc == null){
				afflictionDesc = new AfflictionBean();
			}
		
			
		}
		
		public void initServices() throws CommonException{
			
			if(affSympSrv == null){
				affSympSrv = new AfflicSympServices(this.sqlMap.getConfig());
			}
			
			if(afflicSrv == null){
				afflicSrv = new AfflictionServices(this.sqlMap.getConfig());
			}
			
			if(sympSrv == null){
				sympSrv = new SymptomServices(this.sqlMap.getConfig());
			}
			
			if(usSrv == null){
				usSrv = new UsersServices(this.sqlMap.getConfig());
			}

			
		}
		
		public void initAssistants(){
			
			if(aid == null){
				aid = new WithinHolder();
			}
			
			if(convert == null){
				convert = new ConvertAssistant();
			}
			
			if(dateAssist == null){
				dateAssist = new DateAssistant();
			}
			
			
		}
		
		public void initCollections(){
			
			if(symptomList == null){
				symptomList = new ArrayList<SymptomBean>();
			}
		}
	
	    // Used to clean all objects
	    public void cleanData() throws CommonException{
	    	
	    	// Cleaning Beans
	  		user = null;
	  		afflicSymp = null;
	  		editAfflicSymp = null;

	  		// Cleaning Services
	  		affSympSrv = null;
			usSrv = null;

			// Cleaning Assistants
			aid = null;
			convert = null;
			dateAssist = null;
			
			// Cleaning Collections
			allAfflicSymp = null;
	    	
	    }
	
	
	
	public void clearList(){
		if (symptomList.isEmpty()){
			System.out.println("Empty List");
		}else{
			symptomList.clear();
			
		}
		
	}
	
	public void fillList(){
		System.out.println("Metodo de Llenado ejecutado");
		System.out.println("remove: " + this.getRemove());
		System.out.println("check: " + this.isCheck());
		if(check == true){
			deleteList.add(remove);
		}else{
			if(check == false){
				deleteList.remove(remove);
			}
			
		}
		
		System.out.println(deleteList);
		
	}
	public void removeList(){
		convert = new ConvertAssistant();
		int i;
		int j;
			
		if (symptomList.isEmpty() || deleteList.isEmpty()) {
			System.out.println("Lista Vacia");
		}else{
			System.out.println("Metodo de eliminacion ejecutado");
			System.out.println("Sintomas: " + symptomList);
			System.out.println("A eliminar: " + deleteList);

			for(i = 0; i<this.deleteList.size(); i++){
				
				for(j = 0; j<this.symptomList.size(); j++){
					String desc = convert.getSympDesc(this.symptomList.get(j));
					System.out.println("Descrip: " + desc);
					if((this.deleteList.get(i)).equals(desc)){
						this.symptomList.remove(j);
					}
				}
				
			}
						
			System.out.println("Elementos eliminados");
			System.out.println("Sintomas: " + symptomList);
			System.out.println("A eliminar: " + deleteList);
		}
		this.setInitVar(null);
//		deleteList.clear();
		this.check = false;
	}
	

	// Used to load a grid using a collection
	private void loadGrid() {
		try {
			this.setAllAfflicSymp(affSympSrv.getAllAfflicSymp());
			
		} catch (CommonException e) {
			e.printStackTrace();
		}
	}

	// Used to cancel
	public String cancelData() {
//		this.facesMessages.clear();
		this.setInitVar("S");
		return null;
	}

	// Used to UpperCase
	private String convertField(String data) {
		if (data == null) {
			return null;
		}
		return data.trim().toUpperCase();
	}
		

	// Used for data saving process
	public void saveNewData() throws ParseException, CommonException {
		try {
			this.facesMessages.clear();
			
			AfflictionServices afflicSrv = new AfflictionServices(this.sqlMap.getConfig());
			SymptomServices sympSrv = new SymptomServices(this.sqlMap.getConfig());
			
			AfflicSympBean newAfflicSymp = new AfflicSympBean();
			this.today = DateAssistant.getCurrentDate();
			
		
			System.out.println(convert.getAfflicString(this.getAfflictionDesc()));
			if (this.getAfflictionDesc() == null){
				System.out.println("Descripcion de enfermedad nula");
			}else{
				this.afflictionCod = convert.getAfflicString(this.getAfflictionDesc());
			}
			
			if (afflictionCod != null){
				this.afflictionId = afflicSrv.getIdAffliction(this.getAfflictionCod());
			}else{
				System.out.println("Codigo de enfermedad nulo");
			}
			
			
			this.symptomCod = null;
			this.symptomId= null;
			

			Iterator<SymptomBean> i = symptomList.iterator();
			while(i.hasNext()){
				String cod = convert.getSympString(i.next());
				System.out.println("i:" + cod);
				this.symptomCod = cod;
				System.out.println("Enfermedad:" + this.afflictionCod);
				System.out.println("Sintoma:" + this.symptomCod);
				this.symptomId= sympSrv.getIdSymptom(this.symptomCod);
				
				newAfflicSymp.setAffliction(this.afflictionId);
				newAfflicSymp.setSymptom(this.symptomId);
				newAfflicSymp.setStartDate(this.today);
				System.out.println(this.today);
				newAfflicSymp.setUserIns(user.getId());
				
				affSympSrv.saveNewAfflicSymp(newAfflicSymp);
			}
			
		} catch (CommonException e) {
//			this.facesMessages.add(Severity.ERROR, e.getMessage());
			System.out.println("NO SE PUDO INSERTAR. " + e.getMessage());
			log.error(e);
		}finally{
			
			this.cleanData(); // Cleaning Objects
			this.setInitVar("S"); // Setting initialization Flag
			
		}
	}
	
	// Retrieving data for updating
	public void dataToEdit(Long id){
		System.out.println("id = " + id.toString());
		for (AfflicSympBean bean : this.getAllAfflicSymp()) {
			if(bean.getId() == id){
				this.setEditAfflicSymp(bean);
				return;
			}			
		}	
	}
	
	public void dataToEditUpd() throws ParseException{
		try {
			affSympSrv.updateAfflicSymp(this.getEditAfflicSymp());
		} catch (CommonException e) {
			this.facesMessages.add(Severity.ERROR, e.getMessage());
			System.out.println("NO SE PUDO ACTUALIZAR EL REG. " + e.getMessage());
			log.error(e);
		}
	}
	
	public void dataToDelete(Long id){
		for (AfflicSympBean bean: this.getAllAfflicSymp()){
			if (bean.getId() == id) {
				try {
					affSympSrv.deleteAfflicSympById(bean.getId());
					
				} catch (Exception e) {
					this.facesMessages.add(Severity.ERROR, e.getMessage());
					System.out.println("NO SE BORRAR EL REGISTRO. " + e.getMessage());
					log.error(e);
				}
			}
			
		}
		this.setInitVar("S");
	}


//	GETTERS AND SETTERS
	
	public FacesMessages getFacesMessages() {
		return facesMessages;
	}

	public void setFacesMessages(FacesMessages facesMessages) {
		this.facesMessages = facesMessages;
	}

	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

	public ServiceSqlMap getSqlMap() {
		return sqlMap;
	}

	public void setSqlMap(ServiceSqlMap sqlMap) {
		this.sqlMap = sqlMap;
	}

	public String getInitVar() {
		return initVar;
	}

	public void setInitVar(String initVar) {
		this.initVar = initVar;
	}

	public AfflicSympBean getAfflicSymp() {
		return afflicSymp;
	}

	public void setAfflicSymp(AfflicSympBean afflicSymp) {
		this.afflicSymp = afflicSymp;
	}

	public Collection<AfflicSympBean> getAllAfflicSymp() {
		return allAfflicSymp;
	}

	public void setAllAfflicSymp(Collection<AfflicSympBean> allAfflicSymp) {
		this.allAfflicSymp = allAfflicSymp;
	}

	public AfflicSympBean getEditAfflicSymp() {
		return editAfflicSymp;
	}

	public void setEditAfflicSymp(AfflicSympBean editAfflicSymp) {
		this.editAfflicSymp = editAfflicSymp;
	}

	public AfflictionBean getAfflictionDesc() {
		return afflictionDesc;
	}

	public void setAfflictionDesc(AfflictionBean afflictionDesc) {
		this.afflictionDesc = afflictionDesc;
	}

	public String getAfflictionCod() {
		return afflictionCod;
	}

	public void setAfflictionCod(String afflictionCod) {
		this.afflictionCod = afflictionCod;
	}

	public Long getAfflictionId() {
		return afflictionId;
	}

	public void setAfflictionId(Long afflictionId) {
		this.afflictionId = afflictionId;
	}

	public SymptomBean getSymptomDesc() {
		return symptomDesc;
	}

	public void setSymptomDesc(SymptomBean symptomDesc) {
		this.symptomDesc = symptomDesc;
	}

	public String getSymptomCod() {
		return symptomCod;
	}

	public void setSymptomCod(String symptomCod) {
		this.symptomCod = symptomCod;
	}

	public Long getSymptomId() {
		return symptomId;
	}

	public void setSymptomId(Long symptomId) {
		this.symptomId = symptomId;
	}



	public List<SymptomBean> getSymptomList() {
		return symptomList;
	}



	public void setSymptomList(List<SymptomBean> symptomList) {
		this.symptomList = symptomList;
	}

	
	public List<Object> getDeleteList() {
		return deleteList;
	}

	public void setDeleteList(List<Object> deleteList) {
		this.deleteList = deleteList;
	}

	public boolean isCheck() {
		return check;
	}

	public void setCheck(boolean check) {
		this.check = check;
	}

	public Object getRemove() {
		return remove;
	}

	public void setRemove(Object remove) {
		this.remove = remove;
	}


	public UsersBean getUser() {
		return user;
	}


	public void setUser(UsersBean user) {
		this.user = user;
	}

}
